#ifndef _XBASE_CLIENT_SOCKET_H_
#define _XBASE_CLIENT_SOCKET_H_

#include <string>

#include "XBaseProtocol.h"

#include "xsocket.h"
#include "XBaseLogger.h"

class XBaseSocket
{
protected:
	std::string _ip;
	int    _port;
	XBaseProtocol _protocol;	

	int _socket_fd;

	uint32_t mRecvTimeoutMs;
	uint32_t mSendTimeoutMs;

	int mErrno;
	std::string mErrstr;

public:
	XBaseSocket();	
	virtual ~XBaseSocket();
	
	void init(const std::string& ip, int port, uint32_t recvTimeoutMs=5000, uint32_t sendTimeoutMs=5000);
	bool is_connected();
	bool reconnect();
	void close();

	std::string& ip() { return _ip; }
	int& port() { return _port; }

	int& errorno() { return mErrno; }
	std::string& errorstr() { return mErrstr; }

	void set_recv_timeout(uint32_t timeoutMs);
	void set_send_timeout(uint32_t timeoutMs);

	int send_request(const char* buffer, size_t buffer_size);
	int recv_response(char* buffer, size_t buffer_size);
	
protected:
	XDECL_LOGGER(logger);
};

#endif

